package com.sunny.ynhz.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.HistoryPatrolBean;
import com.sunny.ynhz.ui.patrol.PatrolDetailActivity;
import com.sunny.ynhz.utils.TimeFormatUtils;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luao on 2017/6/7.
 */

public class HistoryPatrolAdapter extends RecyclerView.Adapter<HistoryPatrolAdapter.Myholder> {

    private Context mContext;
    private LinkedList<HistoryPatrolBean.DataBean.RowsBean> mItemLists;

    public HistoryPatrolAdapter(Context mContext, LinkedList<HistoryPatrolBean.DataBean.RowsBean> mItemLists) {
        this.mItemLists = mItemLists;
        this.mContext = mContext;
    }


    @Override
    public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_patrol_history, parent, false);
        Myholder myholder = new Myholder(view);
        return myholder;
    }

    @Override
    public void onBindViewHolder(Myholder holder, int position) {
        final HistoryPatrolBean.DataBean.RowsBean data = mItemLists.get(position);

        holder.patrolTime.setText(TimeFormatUtils.getTime(data.getStrTm(), "yyyy/MM/dd"));// TODO: 2017/6/7 开始时间，要转成日期格式2016/03/03

        //巡河类型 1计划、2交办、3突发事件
        String patrolType = data.getPatrolType();
        int type = 0;
        try {
            type = Integer.parseInt(patrolType);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        holder.patrolMan.setText(data.getPatrolMan());
        holder.patrolDistance.setText(data.getDistance() + "km");
        if (data.getStrTm() != null) {
            holder.strTm.setText(TimeFormatUtils.getTime(data.getStrTm(), "HH:mm"));
        } else if (data.getStrTm() == null) {
            holder.strTm.setText("无");
        }
        if (data.getEndTm() != null) {
            holder.endTm.setText(TimeFormatUtils.getTime(data.getEndTm(), "HH:mm"));
        } else if (data.getEndTm() == null) {
            holder.endTm.setText("无");
        }

        holder.problemCount.setText(data.getZnum() + "个");//问题总数
        holder.solvedProblemCount.setText(data.getWnum() + "个");//尚未解决问题数

        final int finalType = type;
        holder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PatrolDetailActivity.class);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mItemLists.size();
    }


    class Myholder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.patrol_time)
        TextView patrolTime;
        @BindView(R.id.patrol_type)
        TextView patrolType;
        @BindView(R.id.patrol_man)
        TextView patrolMan;
        @BindView(R.id.patrol_distance)
        TextView patrolDistance;
        @BindView(R.id.str_tm)
        TextView strTm;
        @BindView(R.id.end_tm)
        TextView endTm;
        @BindView(R.id.problem_count)
        TextView problemCount;
        @BindView(R.id.solved_problem_count)
        TextView solvedProblemCount;
        @BindView(R.id.content_view)
        RelativeLayout contentView;

        public Myholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View v) {

        }
    }
}
